import {createStore} from '@/airstore';
import ApiClient from '@/services/apiClient';

export default createStore({
  namespace: 'MaterialReturnStore',
  shipOrder: {
    name: '',
  },
  shipDetails: [],
  returnedResult: {},
  setShipOrder(soRes) {
    this.shipOrder =
      //soRes;
      {
        name: 'FL2020150XXY',
      };
    this.shipDetails = [
      {
        id: 1,
        part_no: 'ABS5643AA29-17GN',
        serial_no: '',
        batch_no: '33685',
        name: '地板压条(FLOOR LAYERING)',
        ship_qty: 40,
        returned_qty: 30,
        returning_qty: 0,
        available: false,
      },
      {
        id: 2,
        part_no: 'ABS5643AA29-17GN',
        serial_no: '',
        batch_no: '33685',
        name: '地板压条(FLOOR LAYERING)',
        ship_qty: 4000000,
        returned_qty: 0,
        returning_qty: 0,
        available: false,
      },
      {
        id: 3,
        part_no: 'ABS5643AA29-18GN',
        serial_no: 'asdfjkllbnm',
        batch_no: '33689',
        name: '地板压条(FLOOR LAYERING)',
        ship_qty: 40,
        returned_qty: 40,
        returning_qty: 0,
        available: false,
      },
    ];
  },
  changeReturnDetail(detailId, detailField, newValue) {
    return this.shipDetails.map((shd) => {
      if (shd.id === detailId) {
        shd[detailField] = newValue;
      }
      return shd;
    });
  },
  slotReturnOne(returnedOne) {
    let totalReturnQty = returnedOne.returning_qty;
    this.shipDetails = this.shipDetails.map((shd) => {
      if (
        totalReturnQty === 0 ||
        shd.batch_no !== returnedOne.batch_no ||
        shd.returned_qty + shd.returning_qty === shd.ship_qty
      ) {
        return shd;
      } else if (
        totalReturnQty <=
        shd.ship_qty - shd.returned_qty - shd.returning_qty
      ) {
        shd.returning_qty += totalReturnQty;
        shd.available = returnedOne.available;
        totalReturnQty = 0;
      } else {
        const returningDeltaQty =
          shd.ship_qty - shd.returned_qty - shd.returning_qty;
        shd.returning_qty += returningDeltaQty;
        shd.available = returnedOne.available;
        totalReturnQty -= returningDeltaQty;
      }
      return shd;
    });
  },
  async getMaterialByBarcode(barcode) {
    const srRes = await ApiClient.get('amStore/v1/ScanCodeY', {
      params: {
        apCode: barcode,
      },
    });
    return {batch_no: '33685'};
    //return srRes;
  },
  async getReturnShipOrder(barcode) {
    const rsoRes = await ApiClient.get('amStore/v1/shiporderP', {
      params: {
        apCode: barcode,
      },
    });
    this.setShipOrder(rsoRes.data);
    return;
    if (!rsoRes.errorCode) {
      this.setShipOrder(rsoRes.data);
    }
    return rsoRes.errorMsg;
  },
  async postReturnItems() {
    const rsoRes = await ApiClient.post('amStore/v1/shiporderM', {
      items: this.shipDetails.filter((shp) => shp.returning_qty > 0),
    });
  },
  async getReturnResults() {
    const rsoRes = await ApiClient.get('amStore/v1/shiporderY', {
      params: {
        apCode: this.returnedResult.id,
      },
    });
  },
});
